﻿<div [@routerTransition]>
    <div class="content d-flex flex-column flex-column-fluid">
        <sub-header [title]="'Tenants' | localize" [description]="'TenantsHeaderInfo' | localize">
            <div role="actions">
                <button *ngIf="'Pages.Tenants.Create' | permission" class="btn btn-primary"
                        (click)="createTenant()"><i class="fa fa-plus"></i>{{"CreateNewTenant" | localize}}
                </button>
            </div>
        </sub-header>

        <div [class]="containerClass">
            <div class="card card-custom gutter-b">
                <div class="card-body">
                    <div class="form">
                        <form (submit)="getTenants()" autocomplete="new-password">
                            <div class="form-body">

                                <div class="row mb-4">
                                    <div class="col-xl-6">
                                        <label
                                            for="TenantNameOrTenancyCode">{{'TenantNameOrTenancyCode' | localize}}</label>
                                        <div class="input-group">
                                            <input id="TenantNameOrTenancyCode"
                                                   [(ngModel)]="filters.filterText"
                                                   name="filterText" class="form-control"
                                                   placeholder="{{'SearchWithThreeDot' | localize}}"
                                                   type="text">
                                            <span class="input-group-btn">
                                                <button class="btn btn-primary" type="submit"><i
                                                    class="flaticon-search-1"
                                                    [attr.aria-label]="l('Search')"></i></button>
                                            </span>
                                        </div>
                                    </div>
                                    <div class="col-xl-6">
                                        <label for="EditionDropdown">{{'Edition' | localize}}</label>
                                        <edition-combo
                                            [(selectedEdition)]="filters.selectedEditionId"></edition-combo>
                                    </div>
                                </div>
                                <div class="row mb-4">
                                    <div class="col-xl-6">
                                        <div class="checkbox-list mb-2">
                                            <label class="checkbox">
                                                <input type="checkbox"
                                                       [(ngModel)]="filters.subscriptionEndDateRangeActive"
                                                       id="TenantsTable_SubscriptionEndDateRangeActive"
                                                       name="SubscriptionEndDateRangeActive" value="true">
                                                <span></span>
                                                {{'SubscriptionEndDate' | localize}}
                                            </label>
                                        </div>
                                        <input class="form-control" name="SubscriptionEndDateRange"
                                               [attr.disabled]="!filters.subscriptionEndDateRangeActive ? 'disabled' : null"
                                               #drpSubscriptionEndDateRange="bsDaterangepicker"
                                               bsDaterangepicker
                                               readonly="readonly" dateRangePickerMomentModifier
                                               [(date)]="subscriptionDateRange">
                                    </div>
                                    <div class="col-xl-6">
                                        <div class="checkbox-list mb-2">
                                            <label class="checkbox">
                                                <input type="checkbox"
                                                       [(ngModel)]="filters.creationDateRangeActive"
                                                       id="TenantsTable_CreationDateRangeActive"
                                                       name="CreationDateRangeActive" value="true">
                                                <span></span>
                                                {{'CreationTime' | localize}}
                                            </label>
                                        </div>
                                        <input class="form-control" name="CreationDateRange"
                                               [attr.disabled]="!filters.creationDateRangeActive ? 'disabled' : null"
                                               #drpCreationDateRange="bsDaterangepicker" bsDaterangepicker
                                               readonly="readonly" dateRangePickerMomentModifier
                                               [(date)]="creationDateRange"/>
                                    </div>
                                </div>
                                <div class="row mb-4">
                                    <div class="col-xl-12 text-right">
                                        <button name="RefreshButton" class="btn btn-primary"><i
                                            class="fa fa-sync"></i>
                                            {{'Refresh' | localize}}</button>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="row align-items-center">
                        <!--<Primeng-TurboTable-Start>-->
                        <div class="col primeng-datatable-container"
                             [busyIf]="primengTableHelper.isLoading">
                            <p-table #dataTable sortMode="multiple" (onLazyLoad)="getTenants($event)"
                                     [value]="primengTableHelper.records"
                                     rows="{{primengTableHelper.defaultRecordsCountPerPage}}"
                                     [paginator]="false"
                                     [lazy]="true" [responsive]="primengTableHelper.isResponsive">
                                <ng-template pTemplate="header">
                                    <tr>
                                        <th style="width:130px">{{'Actions' | localize}}</th>
                                        <th pSortableColumn="tenancyName">
                                            {{'TenancyCodeName' | localize}}
                                            <p-sortIcon field="tenancyName"></p-sortIcon>
                                        </th>
                                        <th pSortableColumn="name">
                                            {{'TenantName' | localize}}
                                            <p-sortIcon field="name"></p-sortIcon>
                                        </th>
                                        <th>{{'Edition' | localize}}</th>
                                        <th pSortableColumn="subscriptionEndDateUtc">
                                            {{'SubscriptionEndDateUtc' | localize}}
                                            <p-sortIcon field="subscriptionEndDateUtc"></p-sortIcon>
                                        </th>
                                        <th pSortableColumn="isActive">
                                            {{'Active' | localize}}
                                            <p-sortIcon field="isActive"></p-sortIcon>
                                        </th>
                                        <th pSortableColumn="creationTime">
                                            {{'CreationTime' | localize}}
                                            <p-sortIcon field="creationTime"></p-sortIcon>
                                        </th>
                                    </tr>
                                </ng-template>
                                <ng-template pTemplate="body" let-record="$implicit">
                                    <tr>
                                        <td style="width: 130px">
                                            <div class="btn-group dropdown" dropdown>
                                                <button dropdownToggle
                                                        class="dropdown-toggle btn btn-sm btn-primary">
                                                    <i class="fa fa-cog"></i><span class="caret"></span>
                                                    {{"Actions" | localize}}
                                                </button>
                                                <ul class="dropdown-menu" *dropdownMenu>
                                                    <li [class.disabled]="!record.isActive">
                                                        <a href="javascript:;" class="dropdown-item"
                                                           *ngIf="'Pages.Tenants.Impersonation' | permission"
                                                           [class.disabled]="!record.isActive"
                                                           (click)="record.isActive && showUserImpersonateLookUpModal(record)">{{'LoginAsThisTenant' | localize}}</a>
                                                    </li>
                                                    <li>
                                                        <a href="javascript:;" class="dropdown-item"
                                                           *ngIf="'Pages.Tenants.Edit' | permission"
                                                           (click)="editTenantModal.show(record.id)">{{'Edit' | localize}}</a>
                                                    </li>
                                                    <li>
                                                        <a href="javascript:;" class="dropdown-item"
                                                           *ngIf="'Pages.Tenants.ChangeFeatures' | permission"
                                                           (click)="tenantFeaturesModal.show(record.id, record.name)">{{'Features' | localize}}</a>
                                                    </li>
                                                    <li>
                                                        <a href="javascript:;" class="dropdown-item"
                                                           *ngIf="'Pages.Tenants.Delete' | permission"
                                                           (click)="deleteTenant(record)">{{'Delete' | localize}}</a>
                                                    </li>
                                                    <li>
                                                        <a href="javascript:;" class="dropdown-item"
                                                           (click)="unlockUser(record)">{{'Unlock' | localize}}</a>
                                                    </li>
                                                    <li>
                                                        <a href="javascript:;" *ngIf="entityHistoryEnabled" class="dropdown-item"
                                                           (click)="showHistory(record)">{{'History' | localize}}</a>
                                                    </li>
                                                </ul>
                                            </div>
                                        </td>
                                        <td>
                                                        <span
                                                            class="ui-column-title">{{'TenancyCodeName' | localize}}</span>
                                            <i *ngIf="record.connectionString" class="fa fa-database"
                                               title="{{'HasOwnDatabase' | localize}}"></i>
                                            {{record.tenancyName}}
                                        </td>
                                        <td>
                                            <span class="ui-column-title">{{'Name' | localize}}</span>
                                            {{record.name}}
                                        </td>
                                        <td>
                                            <span class="ui-column-title">{{'Edition' | localize}}</span>
                                            {{record.editionDisplayName}}
                                        </td>
                                        <td>
                                                        <span
                                                            class="ui-column-title">{{'SubscriptionEndDateUtc' | localize}}</span>
                                            {{ record.subscriptionEndDateUtc ? (record.subscriptionEndDateUtc | momentFormat:'L') : '-'}}
                                        </td>
                                        <td>
                                            <span class="ui-column-title">{{'Active' | localize}}</span>
                                            <span *ngIf="record.isActive"
                                                  class="badge badge-success badge-inline">{{'Yes' | localize}}</span>
                                            <span *ngIf="!record.isActive"
                                                  class="badge badge-dark badge-inline">{{'No' | localize}}</span>
                                        </td>
                                        <td>
                                                        <span
                                                            class="ui-column-title">{{'CreationTime' | localize}}</span>
                                            {{record.creationTime | momentFormat:'L'}}
                                        </td>
                                    </tr>
                                </ng-template>
                            </p-table>
                            <div class="primeng-no-data" *ngIf="primengTableHelper.totalRecordsCount == 0">
                                {{'NoData' | localize}}
                            </div>
                            <div class="primeng-paging-container">
                                <p-paginator [rows]="primengTableHelper.defaultRecordsCountPerPage"
                                             #paginator
                                             (onPageChange)="getTenants($event)"
                                             [totalRecords]="primengTableHelper.totalRecordsCount"
                                             [rowsPerPageOptions]="primengTableHelper.predefinedRecordsCountPerPage">
                                </p-paginator>
                                <span class="total-records-count">
                                    {{'TotalRecordsCount' | localize:primengTableHelper.totalRecordsCount}}
                                </span>
                            </div>
                        </div>
                        <!--<Primeng-TurboTable-End>-->
                    </div>
                </div>
            </div>
        </div>

        <createTenantModal #createTenantModal (modalSave)="getTenants()"></createTenantModal>
        <editTenantModal #editTenantModal (modalSave)="getTenants()"></editTenantModal>
        <tenantFeaturesModal #tenantFeaturesModal></tenantFeaturesModal>
        <commonLookupModal #impersonateUserLookupModal
                           (itemSelected)="impersonateUser($event)"></commonLookupModal>
        <entityTypeHistoryModal #entityTypeHistoryModal></entityTypeHistoryModal>
    </div>
</div>
